﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Users_Index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //PermissionHelper hh = new PermissionHelper();
            //Session["Permission"] = hh.GetPermission(EUserRole.superadmin);
            btnCreate.Visible = false;
            if (Session["Permission"] != null)
            {
                Permission permission = Session["Permission"] as Permission;
                if (permission.IsUserEdit)
                {
                    btnCreate.Visible = true;
                    //btnImport.Visible = false;
                    //fuImport.Visible = false;
                }

                Bind(permission);
                

            }
        }
    }

    void Bind(Permission permission)
    {
        UserBLL ubll = new UserBLL(permission);

        bool hasLeader = false;

        if (Request.Params["cid"] != null)
        {
            int companyID = Convert.ToInt32(Request.Params["cid"]);
            gvUsers.DataSource = ubll.GetAllUserView(companyID).Tables[0];

            //获取组管理员
            CompanyBLL cbll = new CompanyBLL(permission);
            DataSet ds = cbll.GetCompanyAssignedByCompanyID(companyID);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                lblLeaderName.Text = "组管理员:";
                string temp = "";
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    temp += SwiftConverter.CovertDBObjectToString(dr["UserName"]) + ",";
                }
                lblLeaderName.Text += temp.Substring(0, temp.Length - 1);
                hasLeader = true;
            }
        }
        else
        {
            gvUsers.DataSource = ubll.GetAllUserView().Tables[0];
        }

        gvUsers.DataBind();

        switch (permission.EUserRole)
        {
            case EUserRole.superadmin:
                gvUsers.Columns[11].Visible = true;
                gvUsers.Columns[12].Visible = hasLeader;
               // gvUsers.Columns[12].Visible = false;
                break;
            case EUserRole.admin:
                gvUsers.Columns[11].Visible = false;
                gvUsers.Columns[12].Visible = hasLeader;
               // gvUsers.Columns[12].Visible = true;
                break;
            case EUserRole.leader:
                gvUsers.Columns[11].Visible = false;
                gvUsers.Columns[12].Visible = hasLeader;
               // gvUsers.Columns[12].Visible = false;
                break;
            default:
                gvUsers.Columns[11].Visible = false;
                gvUsers.Columns[12].Visible = false;
               // gvUsers.Columns[12].Visible = false; 
                break;
        }
    }

    

    protected void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            
            if (e.CommandName == "Edit" && permission.IsUserEdit)
            {
                string uid = ((HiddenField)gvUsers.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("hUserID")).Value;

                if (Request.Params["cid"] != null)
                {
                    Response.Redirect("Edit.aspx?uid=" + uid + "&cid=" + Request.Params["cid"]);
                }
                else
                {
                    Response.Redirect("Edit.aspx?uid=" + uid);
                }

            }

            if (e.CommandName == "Select")
            {
                //选中即跳往相关人员列表
                //string uid = ((HiddenField)gvUsers.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("hUserID")).Value;
                //Response.Redirect("Details.aspx?uid=" + uid);

            }

            if (e.CommandName == "Violation")//违章
            {
                //选中即跳往相关人员列表
                //check
                Response.Redirect("~/Violation/Create.aspx?uid=" + e.CommandArgument);

            }

            
        }
    }

    protected void gvUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            if (!permission.IsUserEdit)
            {

                return;
            }
            string uid = gvUsers.DataKeys[e.RowIndex].Value.ToString();//获取主键  
            UserBLL ubll = new UserBLL(permission);
            ubll.DeleteUser(Convert.ToInt32(uid));
             
            if (Request.Params["cid"] != null)
            {
                Response.Write("<script> alert( '删除成功！ ');location= 'Index.aspx?cid=" + Request.Params["cid"] + "'</script> ");
            }
            else
            {
                Response.Write("<script> alert( '删除成功！ ');location= 'Index.aspx'</script> ");
            }
        
        }
    }
    
    protected void btnImport_Click(object sender, EventArgs e)
    {

    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        if (Request.Params["cid"] != null)
        {
            Response.Redirect("Create.aspx?cid=" + Request.Params["cid"]);
        }
        else
        {
            Response.Redirect("Create.aspx");
        }
    }

    protected void gvUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       

        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            
            gvUsers.PageIndex = e.NewPageIndex;
            Bind(permission);
        }
    }


    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        foreach (GridViewRow row in gvUsers.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                row.Attributes["onmouseover"] =
                   "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';";
                row.Attributes["onmouseout"] =
                   "this.style.backgroundColor=currentcolor";
                // Set the last parameter to True 
                // to register for event validation. 
                //row.Attributes["onclick"] =
                // ClientScript.GetPostBackClientHyperlink(gvUsers,
                //    "Select$" + row.DataItemIndex, true);
            }
        }
        base.Render(writer);
    }
    protected void gvUsers_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Companys/Index.aspx");
    }
}
